package com.dspsemi.wodi;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

	private EditText name;
	private EditText password;
	private Button login;
	private Button register;
	private ProgressDialog pd;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		initViews();
		setListeners();

		SharedPreferences sp = getSharedPreferences("wodi", 0);
		String nameStr = sp.getString("name", "");
		String passwordStr = sp.getString("password", "");
		name.setText(nameStr);
		password.setText(passwordStr);

		// 自动登录
		if (!nameStr.equals("")) {
			login(nameStr, passwordStr);
		}
	}

	@Override
	protected void onRestart() {
		super.onRestart();

		SharedPreferences sp = getSharedPreferences("wodi", 0);
		String nameStr = sp.getString("name", "");
		String passwordStr = sp.getString("password", "");
		name.setText(nameStr);
		password.setText(passwordStr);

	}

	private void initViews() {
		name = (EditText) findViewById(R.id.name);
		password = (EditText) findViewById(R.id.password);
		login = (Button) findViewById(R.id.login);
		register = (Button) findViewById(R.id.register);

		pd = new ProgressDialog(this);
		pd.setMessage("登录中");
		pd.setCancelable(false);
	}

	private void setListeners() {
		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String nameStr = name.getText().toString();
				String passwordStr = password.getText().toString();

				if (nameStr == null || nameStr.length() == 0) {
					toast("请输入帐号");
					return;
				}
				if (passwordStr == null || passwordStr.length() == 0) {
					toast("请输入密码");
					return;
				}

				login(nameStr, passwordStr);

			}
		});

		register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent();
				i.setClass(LoginActivity.this, RegisterActivity.class);
				startActivity(i);
			}
		});
	};

	protected void login(String nameStr, String passwordStr) {
		pd.show();
		new Thread() {
			public void run() {
				mHandler.sendEmptyMessage(1);
			};
		}.start();
	}

	private void toast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			pd.dismiss();
			if (msg.what == 1) {
				// 登录成功

				String nameStr = name.getText().toString();
				String passwordStr = password.getText().toString();

				SharedPreferences sp = getSharedPreferences("wodi", 0);
				sp.edit().putString("name", nameStr).commit();
				sp.edit().putString("password", passwordStr).commit();
				sp.edit().putString("nickname", "").commit();// FIXME
				sp.edit().putString("jifen", "5555").commit();// FIXME
				sp.edit().putString("sheng", "800").commit();// FIXME
				sp.edit().putString("bai", "200").commit();// FIXME

				String nickname = sp.getString("nickname", "");
				if (nickname.equals("")) {
					// 信息不完善的话要先完善信息
					Intent i = new Intent();
					i.setClass(LoginActivity.this, InitInfoActivity.class);
					startActivity(i);
				} else {
					Intent i = new Intent();
					i.setClass(LoginActivity.this, HallActivity.class);
					startActivity(i);
				}

				return;
			}
			if (msg.what == 0) {
				toast("登录失败");
				return;
			}
		};
	};
}
